#pragma once

#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <fstream>

#include "../comm/util.hpp"
#include "../comm/log.hpp"

namespace ns_model
{
    using namespace std;
    using namespace ns_util;
    using namespace ns_log;

    struct Question
    {
        string number; // 题目编号, 唯一
        string title;  // 题目的标题
        string star;   // 题目的难度
        int cpu_limit; // 题目的时间要求(S)
        int mem_limit; // 题目的空间要求(KB)
        string desc;   // 题目的描述
        string header; // 题目预设给用户在线编辑器的代码
        string tail;   // 题目的测试用例, 需要和 header 拼接, 形成完整代码
    };

    const string question_list = "./questions/questions.list";
    const std::string questins_path = "./questions/";

    class Model
    {
    private:
        // 题号 : 题目细节
        unordered_map<string, Question> questions;
    public:
        Model()
        {
            assert(LoadQuestionList(question_list));
        }
        bool LoadQuestionList(const string &question_list)
        {
            // 加载配置文件: questions/question.list + 题目编号文件
            ifstream in(question_list);
            if (!in.is_open())
            {
                LOG(FATAL) << " 加载题库失败,请检查是否存在题库文件" << "\n";
                return false;
            }

            string line;
            while (getline(in, line))
            {
                vector<string> tokens;
                StringUtil::SpiltString(line, &tokens, " ");
                if (tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败, 请检查文件格式" << "\n";
                    continue;
                }

                Question q;
                q.number = tokens[0];
                q.title  = tokens[1];
                q.star   = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());

                string path = questins_path;
                path += q.number;
                path += "/";

                FileUtil::ReadFile(path + "desc.txt", &q.desc, true);
                FileUtil::ReadFile(path + "header.cpp", &q.header, true);
                FileUtil::ReadFile(path + "tail.cpp", &q.tail, true);

                questions.insert(make_pair(q.number, q));
            }
            LOG(INFO) << "加载题库...成功!" << "\n";
            in.close();

            return true;
        }
        bool GetAllQuestions(vector<Question> *out)
        {
            if (questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败" << "\n";
                return false;
            }
            for (const auto &q : questions)
            {
                out->push_back(q.second);
            }
            return true;
        }
        bool GetOneQuestion(const string &number, Question *q)
        {
            const auto &iter = questions.find(number);
            if (iter == questions.end())
            {
                LOG(ERROR) << "用户获取编号为 %d " << number << "题目失败" << "\n";
                return false;
            }

            *q = iter->second;
            return true;
        }
        ~Model() {}
    };

}
